﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DLL;

namespace QuanLyBanLe.Admin
{
    public partial class FormProductInfoReport : Form
    {
        private ProductService m_ProductService;
        private ProductTypeService m_ProductTypeService;
        private ProducerService m_ProducerService;

        public FormProductInfoReport()
        {
            InitializeComponent();
            m_ProductService = new ProductService();
            m_ProductTypeService = new ProductTypeService();
            m_ProducerService = new ProducerService();
        }

        private void FormProductInfo_Load(object sender, EventArgs e)
        {
            IList<ProductType> productTypes = m_ProductTypeService.GetProductTypes();
            ProductType allProductType = new ProductType();
            allProductType.Id = 0;
            allProductType.Name = "Tất cả";
            productTypes.Insert(0, allProductType);
            cboProductType.DataSource = productTypes;
            cboProductType.ValueMember = "Id";
            cboProductType.DisplayMember = "Name";

            IList<Producer> producers = m_ProducerService.GetProducers();
            Producer allProducer = new Producer();
            allProducer.Id = 0;
            allProducer.Name = "Tất cả";
            producers.Insert(0, allProducer);
            cboProducer.DataSource = producers;
            cboProducer.ValueMember = "Id";
            cboProducer.DisplayMember = "Name";

            IList<Product> products = m_ProductService.GetProducts();
            products = products.Where(x => x.SalePrice > 0).ToList();
            Product allProduct = new Product();
            allProduct.Id = 0;
            allProduct.Name = "Tất cả";
            products.Insert(0, allProduct);
            cboProduct.DataSource = products;
            cboProduct.ValueMember = "Id";
            cboProduct.DisplayMember = "Name";

            this.Text += FormMain.GetShopInfo();
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            IList<Product> products = new List<Product>();
            int productId = Convert.ToInt32(cboProduct.SelectedValue);
            if (productId > 0)
            {
                Product product = m_ProductService.GetProductById(productId);
                products.Add(product);
            }
            else
            {
                int productTypeId = Convert.ToInt32(cboProductType.SelectedValue);
                int producerId = Convert.ToInt32(cboProducer.SelectedValue);
                products = m_ProductService.GetProductsByTypeAndProducer(productTypeId, producerId);
            }

            ProductBindingSource.DataSource = products.Where(x => x.SalePrice > 0).OrderBy(x => x.ProductTypeId).ThenBy(x => x.Name).ToList();
            this.reportViewer1.RefreshReport();
        }

        private void cbo_KeyDown(object sender, KeyEventArgs e)
        {
            ComboBox cbo = sender as ComboBox;
            if (cbo.DroppedDown)
            {
                cbo.DroppedDown = false;
            }
        }
    }
}
